package zry.Achievement.Report;

import zry.Search.SearchCriteriaReport;
import zry.Util.DBUtil;
import Connection.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ReportDAOImpl extends DAOBase implements ReportDAO {
    private static final String REPORT_INSERT_SQL = "INSERT INTO report_table(achievement_id,report_name,report_type,report_dept,report_time,contribution,supporting) VALUES(?,?,?,?,?,?,?)";
    private static final String REPORT_UPDATE_SQL = "UPDATE report_table SET report_name=?,report_type=?,report_dept=?,report_time=?,contribution=?,supporting=? WHERE achievement_id=?";
    private static final String REPORT_SELECT_SQL = "SELECT * FROM report_table WHERE achievement_id=?";
    private static final String REPORT_SELECT_ALL_SQL = "SELECT * FROM report_table";

    @Override
    public boolean addReport(Report report) {
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(REPORT_INSERT_SQL);
            psmt.setString(1, DBUtil.fixSqlFieldValue(report.getA_id()));
            psmt.setString(2, DBUtil.fixSqlFieldValue(report.getR_name()));
            psmt.setString(3, DBUtil.fixSqlFieldValue(report.getR_type()));
            psmt.setString(4, DBUtil.fixSqlFieldValue(report.getR_dept()));
            psmt.setDate(5, report.getR_time());
            psmt.setString(6, DBUtil.fixSqlFieldValue(report.getR_contribution()));
            psmt.setString(7, DBUtil.fixSqlFieldValue(report.getR_supporting()));
            psmt.executeUpdate();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    @Override
    public boolean updateReport(Report report) {
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(REPORT_UPDATE_SQL);
            psmt.setString(1, DBUtil.fixSqlFieldValue(report.getR_name()));
            psmt.setString(2, DBUtil.fixSqlFieldValue(report.getR_type()));
            psmt.setString(3, DBUtil.fixSqlFieldValue(report.getR_dept()));
            psmt.setDate(4, report.getR_time());
            psmt.setString(5, DBUtil.fixSqlFieldValue(report.getR_contribution()));
            psmt.setString(6, DBUtil.fixSqlFieldValue(report.getR_supporting()));
            psmt.setString(7, DBUtil.fixSqlFieldValue(report.getA_id()));
            psmt.executeUpdate();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    @Override
    public Report getReport(String achievement_id) {
        Report report = null;
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(REPORT_SELECT_SQL);
            psmt.setString(1, achievement_id);
            ResultSet rs = psmt.executeQuery();
            if (rs.next()) {
                report = new Report();
                report.setA_id(rs.getString("achievement_id"));
                report.setR_name(rs.getString("report_name"));
                report.setR_type(rs.getString("report_type"));
                report.setR_dept(rs.getString("report_dept"));
                report.setR_time(rs.getDate("report_time"));
                report.setR_contribution(rs.getString("contribution"));
                report.setR_supporting(rs.getString("supporting"));
            }
            rs.close();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return report;
    }

    public List<Report> getAllReports() {
        List<Report> reports = new ArrayList<>();
        Connection con = null;
        try {
            con = getConnection();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(REPORT_SELECT_ALL_SQL);
            while (rs.next()) {
                Report report = new Report();
                report.setA_id(rs.getString("achievement_id"));
                report.setR_name(rs.getString("report_name"));
                report.setR_type(rs.getString("report_type"));
                report.setR_dept(rs.getString("report_dept"));
                report.setR_time(rs.getDate("report_time"));
                report.setR_contribution(rs.getString("contribution"));
                report.setR_supporting(rs.getString("supporting"));
                reports.add(report);
            }
            rs.close();
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return reports;
    }

    @Override
    public List<Report> findReports(SearchCriteriaReport searchCriteriaReport) {
        List<Report> reports = new ArrayList<>();
        Connection con = null;
        try {
            // 构建SQL语句
            StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM report_table WHERE 1=1");
            if (searchCriteriaReport.getA_id() != null) {
                sqlBuilder.append(" AND achievement_id LIKE ?");
            }
            if (searchCriteriaReport.getR_name() != null) {
                sqlBuilder.append(" AND report_name LIKE ?");
            }
            if (searchCriteriaReport.getR_type() != null) {
                sqlBuilder.append(" AND report_type LIKE ?");
            }
            if (searchCriteriaReport.getR_dept() != null) {
                sqlBuilder.append(" AND report_dept LIKE ?");
            }
            if (searchCriteriaReport.getR_time() != null) {
                sqlBuilder.append(" AND report_time LIKE ?");
            }
            if (searchCriteriaReport.getR_contribution() != null) {
                sqlBuilder.append(" AND contribution LIKE ?");
            }
            if (searchCriteriaReport.getR_supporting() != null) {
                sqlBuilder.append(" AND supporting LIKE ?");
            }
            String sql = sqlBuilder.toString();

            // 执行查询
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(sql);
            int index = 1;
            if (searchCriteriaReport.getA_id() != null) {
                psmt.setString(index++, searchCriteriaReport.getA_id());
            }
            if (searchCriteriaReport.getR_name() != null) {
                psmt.setString(index++, searchCriteriaReport.getR_name());
            }
            if (searchCriteriaReport.getR_type() != null) {
                psmt.setString(index++, searchCriteriaReport.getR_type());
            }
            if (searchCriteriaReport.getR_dept() != null) {
                psmt.setString(index++, searchCriteriaReport.getR_dept());
            }
            if (searchCriteriaReport.getR_time() != null) {
                psmt.setDate(index++, searchCriteriaReport.getR_time());
            }
            if (searchCriteriaReport.getR_contribution() != null) {
                psmt.setString(index++, searchCriteriaReport.getR_contribution());
            }
            if (searchCriteriaReport.getR_supporting() != null) {
                psmt.setString(index++, searchCriteriaReport.getR_supporting());
            }
            ResultSet rs = psmt.executeQuery();
            while (rs.next()) {
                String a_id = rs.getString("achievement_id");
                String r_name = rs.getString("report_name");
                String r_type = rs.getString("report_type");
                String r_dept= rs.getString("report_dept");
                Date r_time = rs.getDate("report_time");
                String r_contribution = rs.getString("contribution");
                String r_supporting = rs.getString("supporting");
                Report report = new Report(a_id, r_name, r_type, r_dept, r_time, r_contribution, r_supporting);
                reports.add(report);
            }
            rs.close();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return reports;
    }
}
